package com.alone.mybatisplus.demo;

import com.alone.mybatisplus.datascope.core.DataScopeInnerInterceptor;
import com.alone.mybatisplus.datascope.core.loader.DataScopeLoader;
import com.alone.mybatisplus.datascope.core.loader.EmptyHandler;
import com.alone.mybatisplus.datascope.core.loader.RoutingDataScopeLoader;
import com.alone.mybatisplus.datascope.mapper.DataScopeSqlInjector;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Alone
 */
@Configuration
public class MybatisPlusConfig {

    /**
     * mybatis plus 拦截器配置
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        // 数据权限 注意!!! 要优于分页处理或者其他查询类型的InnerInterceptor
        DataScopeInnerInterceptor dataScopeInnerInterceptor = new DataScopeInnerInterceptor(dataScopeLoader());
        interceptor.addInnerInterceptor(dataScopeInnerInterceptor);

        // 分页支持
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        interceptor.addInnerInterceptor(paginationInnerInterceptor);

        return interceptor;
    }


    @Bean
    public DataScopeLoader dataScopeLoader() {
        return new RoutingDataScopeLoader();
    }

    @Bean
    public EmptyHandler emptyDataScopeHandler() {
        return new EmptyHandler();
    }

    @Bean
    public DataScopeSqlInjector dataScopeSqlInjector() {
        return new DataScopeSqlInjector();
    }

}

